//
//  ViewController.m
//  20503网络获取图片
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *arr;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;

@property(nonatomic,strong)NSMutableArray *mArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableArray *)mStr
{
    if (_mArr == nil) {
        _mArr = [NSMutableArray array];
    }
    return _mArr;
    
}

- (IBAction)btnPressed:(id)sender
{
    [self.activityView startAnimating];
    
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    dispatch_async(queue, ^{
        
        [self addImageData];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            for (int i = 0; i < self.mArr.count  ; i++ ) {
                
                int row = i/3;
                int colume = i%3;
                NSLog(@"我要创建图片了");
                UIImageView *imageView = [UIImageView new];
                [imageView setFrame:CGRectMake(10+100*colume, 150+90*row, 90, 90)];
                imageView.image = self.mArr[i];
                [self.view addSubview:imageView];
                
                [self.activityView stopAnimating];
            }
        });
        
    });
    
    
}

-(void)addImageData{
    
    
    for (NSString *str in self.arr) {
        NSURL *url = [NSURL URLWithString:str];
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 图像对象
        UIImage *image = [UIImage imageWithData:data];
        [self.mStr addObject:image];
        
    }
    
    
}

@end
